package org.rsbot.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class UIDData
{
  private static String newline = System.getProperty("line.separator");
  private static String separator = "#";

  private HashMap<String, byte[]> uids = new HashMap();
  private String lastUsed = "";

  public UIDData()
  {
    File localFile = new File(GlobalConfiguration.Paths.getUIDsFile());
    if (!localFile.exists()) {
      return;
    }
    try
    {
      BufferedReader localBufferedReader = new BufferedReader(new FileReader(localFile));
      String str;
      while ((str = localBufferedReader.readLine()) != null) {
        if (str.isEmpty()) {
          continue;
        }
        String[] arrayOfString = str.split(separator, 2);
        this.uids.put(arrayOfString[0], arrayOfString[1].getBytes());
      }
    }
    catch (Exception localException) {
    }
  }

  public String getLastUsed() {
    return this.lastUsed;
  }

  public byte[] getUID(String paramString)
  {
    if (paramString.equals("")) {
      paramString = "DEFAULT";
    }
    this.lastUsed = paramString;

    byte[] arrayOfByte = (byte[])this.uids.get(paramString);
    if (arrayOfByte == null) {
      return new byte[0];
    }
    return arrayOfByte;
  }

  public void setUID(String paramString, byte[] paramArrayOfByte)
  {
    if (paramString.equals("")) {
      paramString = "DEFAULT";
    }
    this.uids.put(paramString, paramArrayOfByte);
  }

  public void save()
  {
    try {
      File localFile = new File(GlobalConfiguration.Paths.getUIDsFile());
      if (!localFile.exists()) {
        localFile.createNewFile();
      }
      FileOutputStream localFileOutputStream = new FileOutputStream(localFile);
      for (Iterator localIterator = this.uids.keySet().iterator(); localIterator.hasNext(); )
      {
        String str = (String)localIterator.next();

        localFileOutputStream.write(str.getBytes());
        localFileOutputStream.write(separator.getBytes());
        localFileOutputStream.write((byte[])this.uids.get(str));

        localFileOutputStream.write(newline.getBytes());
      }

      localFileOutputStream.close();
    }
    catch (Exception localException)
    {
    }
  }
}